package top.haijunit.work.converter;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.NullValueMappingStrategy;
import org.mapstruct.ReportingPolicy;
import org.springframework.util.CollectionUtils;
import top.haijunit.work.modules.repo.domain.DeptInfo;
import top.haijunit.work.modules.repo.domain.PositionInfo;
import top.haijunit.work.modules.repo.domain.UserInfo;
import top.haijunit.work.modules.repo.entity.DepartmentEntity;
import top.haijunit.work.modules.repo.entity.PositionEntity;
import top.haijunit.work.modules.repo.entity.UserEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * 【说明类功能】
 *
 * @author zhanghaijun
 * @since 2024-08-27
 */
@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE, nullValueMappingStrategy = NullValueMappingStrategy.RETURN_DEFAULT)
public interface RepoConverter {

    // 职位转换
    @Mapping(target = "positionCode", source = "uniqCode")
    PositionInfo convert(PositionEntity positionEntity);

    @Mapping(target = "id", ignore = true)
    @Mapping(target = "createTime", ignore = true)
    @Mapping(target = "createBy", ignore = true)
    @Mapping(target = "updateTime", ignore = true)
    @Mapping(target = "updateBy", ignore = true)
    @Mapping(target = "deleted", ignore = true)
    @Mapping(target = "uniqCode", source = "positionCode")
    PositionEntity convert(PositionInfo positionInfo);

    default List<PositionInfo> convertPost(List<PositionEntity> sourceList) {
        return (CollectionUtils.isEmpty(sourceList) ? new ArrayList<>() : sourceList.stream().map(this::convert).toList());
    }

    // 部门转换
    @Mapping(target = "parentId", ignore = true)
    @Mapping(target = "children", ignore = true)
    @Mapping(target = "deptCode", source = "uniqCode")
    DeptInfo convert(DepartmentEntity entity);

    @Mapping(target = "id", ignore = true)
    @Mapping(target = "createTime", ignore = true)
    @Mapping(target = "createBy", ignore = true)
    @Mapping(target = "updateTime", ignore = true)
    @Mapping(target = "updateBy", ignore = true)
    @Mapping(target = "deleted", ignore = true)
    @Mapping(target = "uniqCode", source = "deptCode")
    DepartmentEntity convert(DeptInfo info);

    default List<DeptInfo> convertDept(List<DepartmentEntity> sourceList) {
        return (CollectionUtils.isEmpty(sourceList) ? new ArrayList<>() : sourceList.stream().map(this::convert).toList());
    }

    // 用户转换
    @Mapping(target = "userCode", source = "uniqCode")
    UserInfo convert(UserEntity user);

    @Mapping(target = "id", ignore = true)
    @Mapping(target = "createTime", ignore = true)
    @Mapping(target = "createBy", ignore = true)
    @Mapping(target = "updateTime", ignore = true)
    @Mapping(target = "updateBy", ignore = true)
    @Mapping(target = "deleted", ignore = true)
    @Mapping(target = "uniqCode", source = "userCode")
    UserEntity convert(UserInfo user);

    default List<UserInfo> convertUser(List<UserEntity> sourceList) {
        return (CollectionUtils.isEmpty(sourceList) ? new ArrayList<>() : sourceList.stream().map(this::convert).toList());
    }
}
